package com.g4.shoppingback.controller;


import com.g4.shoppingback.common.Result;
import com.g4.shoppingback.pojo.AddressInfo;
import com.g4.shoppingback.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//跨域
@CrossOrigin
@RestController
public class AddressController {

    @Autowired
    private AddressService addressService;


    /**
     * 增加地址
     *
     * @param address
     * @param userId
     * @return
     */
    @PostMapping("/addAddress/{address}/{userId}")
    public Result addAddress(@PathVariable("address") String address,
                             @PathVariable("userId") int userId) {
        if (address != null && address.trim() != " ") {

            addressService.addAddressInfo(address, userId);


            return new Result(true, "操作成功");

        }

        return new Result(false, "操作失败");

    }


    /**
     * 删除地址
     *
     * @param id
     * @return
     */
    @DeleteMapping("/deleteAddress/{addressId}")
    public Result deletedAddress(@PathVariable("addressId") int id) {

        addressService.deleteAddress(id);

        return new Result(true, "操作成功");

    }


    /**
     * 更新地址
     *
     * @param id
     * @param address
     * @param userId
     * @return
     */
    @PostMapping("/updateAddress/{addressId}/{address}/{userId}")
    public Result updateAddress(@PathVariable("addressId") int id,
                                @PathVariable("address") String address,
                                @PathVariable("userId") int userId) {

        if (address == null) {
            return new Result(false, "操作失败");

        }
        addressService.updateAddress(id, address, userId);

        return new Result(true, "操作成功");

    }


    @GetMapping("/selectAddress/{userId}")
    public Result selectAll(@PathVariable("userId") int userId){

        List<AddressInfo> infos = addressService.selectAddress(userId);
        return new Result(true, "操作成功",infos);
    }

}
